// gjw/pages/order/address.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    items:[{id:'1',value:'设为默认'}],
    address_list:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
		app.util.request({
			url: 'entry/wxapp/Isregister',
			data: { openid: wx.getStorageSync('openid') },
			success: function (res) {
				
				if (res.data.code == 200) {
					that.get_list()
				} else {
					wx.redirectTo({
						url: '../index/register',
					})
				}
			}
		})
  },

  // 获取地址列表
  get_list:function(e){
    var that=this
    var openid = wx.getStorageSync('openid')
    app.util.request({
      url:'entry/wxapp/Addresslist',
      data:{
        openid:openid
      },
      success:function(res){
        
        if(res.data.code==200){
          that.setData({
            address_list:res.data.res
          })
        }else if(res.data.code==500){
          wx.showModal({
            title: '提示',
            content: '您暂时还没有收货地址，立刻去新建收货地址吧~',
            success: function (res) {
              if (res.confirm) {
                
                wx.navigateTo({
                  url: '../order/add_area',
                })
              } else if (res.cancel) {
                wx.navigateBack({
                  delta:1
                })
              }
            }
          })
        }
        
      }
    })
  },

  radioChange: function (e) {
    var that=this
    var openid = wx.getStorageSync('openid')
    var d_id = e.detail.value
    app.util.request({
      url:'entry/wxapp/Addressstate',
      data:{
        openid:openid,id:d_id
      },
      success:function(res){
        
        that.get_list()
      }
    })
  },
  send:function(e){
    
     var areaInfo=e.currentTarget.dataset
    wx.navigateBack({
      delta:1
    })
    wx.setStorageSync('areaInfo', areaInfo)
  },

  
  delete:function(e){
    var that=this
    
    var openid = wx.getStorageSync('openid')    
    var d_id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确定删除该地址吗？',
      success: function (res) {
        if (res.confirm) {
          
          app.util.request({
            url: 'entry/wxapp/Addressdel',
            data: { id: d_id,openid:openid },
            success: function (res) {
              
              if(res.data.code==200){
                wx.showToast({
                  title: '删除成功',
                  icon:'success'
                })
                if (res.data.res.length==0){
                  wx.showModal({
                    title: '提示',
                    content: '您暂时还没有收货地址，立刻去新建收货地址吧~',
                    showCancel:false,
                    success: function (res) {
                      if (res.confirm) {
                        
                        wx.navigateTo({
                          url: '../order/add_area',
                        })
                      } 
                      // else if (res.cancel) {
                      //   console.log('用户点击取消')
                      //   wx.navigateBack({
                      //     delta: 1
                      //   })
                      // }
                    }
                  })
                }
                that.setData({
                  address_list: res.data.res
                })
              }
            }
          })
        } else if (res.cancel) {
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.get_list()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})